# -*- coding:utf-8 -*-
"""
Created on 2010-1-20

@author: Administrator
"""
from core import Port,Relation,Module
 
class Clutch(Module):
    '''
    li he qi. state track has problem!
    '''
    def __init__(self,name=None):
        u = Port('u')
        v = Port('v')
        Module.__init__(self,u,v)
        if name:
            self.name = name
        self.setControl(0) #control 0 stands for disclutch
#        self.addmRelations(u,u)
#        self.addmRelations(v,v)
    
    def clutch(self,isClutch=True):
        '''
        enable the clutch when isClutch is True, while disable when isClutch is False
        '''
        self.setControl(isClutch and 1 or 0)
        if isClutch: #state from disclutch to clutch
            self.removenRelations()
            e1 = Relation(self.getPort('u'),self.getPort('v'))
            self.addnRelations(e1)
            
            self.removemRelations()
            e2 = Relation(self.getPort('u'),self.getPort('v'))
            self.addmRelations(e2)
            
        else: #state from clutch to disclutch
            self.removenRelations()
            
            self.removemRelations()
            e2 = Relation(self.getPort('u'))
            e3 = Relation(self.getPort('v'))
            self.addmRelations(e2,e3)            
    
#    def __repr__(self):
#        return repr(Module.__repr__(self))

class Brake(Module):
    '''
    zhi dong qi
    '''
    def __init__(self,name=None):
        z = Port('z')
        z.setType(0) #set brank module's port type to 0, stands for the relation which contains the vetex is not exists 
        Module.__init__(self,z)
        if name:
            self.name = name
        self.setControl(0) #state 0 stands for disbrake
        e1 = Relation(z)
        self.addnRelations(e1)
    
    def brake(self,isBrake=True):
        self.setControl(isBrake and 1 or 0)
        if self.stateChanged() and isBrake:
            self.removemRelations()
            self.getPort('z').setType(1)
            
        elif self.stateChanged():
            self.getPort('z').setType(0)
            self.removemRelations()
            e = Relation(self.getPort('z'))
            self.addmRelations(e)
          
class Engine(Module):
    '''
    fa dong ji
    '''
    def __init__(self,name=None):
        e = Port('e')
        Module.__init__(self,e)
        if name:
            self.name = name
#        self.setState(0)   
        e1 = Relation(e)
        self.addnRelations(e1)
        e2 = Relation(e)
        self.addmRelations(e2)
        

class Gear2(Module):
    '''
    1 dui chi lun, 1 input and 1 output
    '''
    def __init__(self,name=None):
        a = Port('i')
        b = Port('o')
        Module.__init__(self,a,b)
        if name:
            self.name = name
        e1 = Relation(a,b)
        self.addnRelations(e1)
        
        e2 = Relation(a,b)
        self.addmRelations(e2)
    
class Gear3(Module):
    '''
    1 dui chi lun, 1 input and 2 output, fen dong qi
    '''
    def __init__(self,name=None):
        i = Port('i')
        o1 = Port('o1')
        o2 = Port('o2')
        Module.__init__(self,i,o1,o2)
        if name:
            self.name = name
        e1 = Relation(i,o1)
        e2 = Relation(i,o2)
        self.addnRelations(e1,e2)
        
        e3 = Relation(i,o1,o2)
        self.addmRelations(e3)
    
class Converter(Module):
    '''
    bian ju qi
    '''
    def __init__(self,name=None):
        p = Port('p')
        w = Port('w')
        Module.__init__(self,p,w)
        if name:
            self.name = name
        self.setControl(0) #state 1 stands for lock, while 0 stands for unlock
        self.lock(True)
        
    def lock(self,isLock=True):
        '''
        isLock is True when the torque converter is lock
        '''
        self.setControl(isLock and 1 or 0)
        if self.stateChanged() and isLock:
            self.removenRelations()
            e1 = Relation(self.getPort('p'),self.getPort('w'))
            self.addnRelations(e1)
            
            self.removemRelations()
            e2 = Relation(self.getPort('p'),self.getPort('w'))
            self.addmRelations(e2)
        elif self.stateChanged():
            self.removenRelations()
            e1 = Relation(self.getPort('p'),self.getPort('w'))
            self.addnRelations(e1)
            
            self.removemRelations()
            e2 = Relation(self.getPort('p'),self.getPort('w'))
            self.addmRelations(e2)
    
class Planet(Module):
    '''
    xing xing pai
    '''
    def __init__(self,name=None):
        t = Port('t')
        q = Port('q')
        j = Port('j')
        Module.__init__(self,t,q,j)
        if name:
            self.name = name
        e1 = Relation(t,q,j)
        self.addnRelations(e1)
        
        e2 = Relation(t,q)
        e3 = Relation(t,j)
        self.addmRelations(e2,e3)


